package evs.sample;

import java.util.Collections;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class OccupationValidator 
    implements ConstraintValidator<OccupationConstraint, Occupation>
{

    @Override
    public void initialize( OccupationConstraint arg0 )
    {
        
    }

    @Override
    public boolean isValid( Occupation occupation, ConstraintValidatorContext ctx )
    {
        Company company = occupation.getCompany();
        
        if ( company.getFounded() != null )
        {
            if ( occupation.getFrom().before( company.getFounded() ) )
                return false;
        }
        
        if ( company.getAbandoned() != null )
        {
            if ( occupation.getTo().after( company.getAbandoned() ) )
                return false;
        }
        
        Person person = occupation.getPerson();
        
        if ( Collections.disjoint( person.getSkills(), company.getRequiredSkills() ) )
            return false;
        
        return true;
    }

}
